function addScript(url){
  let hm = document.createElement("script");
  hm.src = url;
  let s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(hm, s);
}
if(!window.Qs){
  addScript('https://cdn.bootcss.com/qs/6.5.1/qs.min.js')
}
if(!window.vant){
  addScript('https://cdn.jsdelivr.net/npm/vant@2.0/lib/vant.min.js')
}
if(!window.axios){
  addScript('https://unpkg.com/axios/dist/axios.min.js')
}
// axios config

// 设置默认请求地址
let URL = 'http://xyz.leaf-tech.net/';
//axios是否允许跨域携带Cookie
axios.defaults.withCredentials = false;

//发送一般请求
const http = axios.create({
  timeout: 5000,
  baseURL: URL,
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
  }
})
// 提交json数据
const jsonData = axios.create({
  timeout: 5000,
  baseURL: URL,
  headers: {
    'Content-Type': 'application/json;charset=utf-8',
  }
})
// 提交表单
const formData = axios.create({
  timeout: 100000,
  baseURL: URL,
  headers: {
    'Content-Type': 'multipart/form-data'
  }
})
//添加请求拦截器
http.interceptors.request.use(config => {
  // Do something before request is sent
  // console.log(config)
  return config
}, err => {
  return Promise.reject(err);
})

// GET 请求
function get( url,params = {} ){
  return new Promise (async (resolve, reject) => {
    try {
      const data = await http.get(url,{params})
      const code = Number(data.data.code)
      const desc = data.data.desc
      if (code != 1) {
        // vant.Toast(desc)
      }
      resolve(data.data)
    }
    catch(err) {
      console.log(err)
      vant.Toast('网络错误,请稍后再试')
    }
  })
};
// POST 请求
function post(url, params = {}, back = true) {
  return new Promise(async (resolve, reject) => {
    try {
      const data = await http.post(url, Qs.stringify(params))
      const code = Number(data.data.code)
      const desc = data.data.desc
      if (code != 1) {
        // vant.Toast(desc)
      }
      resolve(data.data)
    } catch (err) {
      console.log(err)
      // vant.Toast('网络错误,请稍后再试')
    }

  })
};
// JSON数据 请求
function json(url, params = {}, back = true) {
  return new Promise(async (resolve, reject) => {
    try {
      const data = await jsonData.post(url, JSON.stringify(params))
      const code = Number(data.data.code)
      const desc = data.data.desc
      if (code != 1) {
        vant.Toast(desc)
      }
      resolve(data.data)
    } catch (err) {
      console.log(err)
      vant.Toast('网络错误,请稍后再试')
    }

  })
};
// form 请求
function form(url, params = {}) {
  return new Promise(async (reslove, reject) => {
    try {
      const data = await formData.post(url, params)
      const code = Number(data.data.code)
      const desc = data.data.desc
      if (code != 1) {
        vant.Toast(desc)
      }
      reslove(data.data)
    } catch (err) {
      console.log(err)
      vant.Toast('网络错误,请稍后再试')
    }
  })
};

class API{

}
// api
let api = {
  // 分享的班级介绍页
  getIntroduce: params => {
    return get("class/introduceShare.html", params);
  },
  // 分享试听详情 | 上课详情
  getListShare: params => {
    return get("file/listShare.html", params);
  },
  getVideo: params =>{
    return get("/file/watchShare.html", params);
  }
}
